package com.ccrfid.rmc6.repository.alarm;

import com.ccrfid.rmc6.entity.alarm.AlarmNotifyRecordEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AlarmNotifyRecordRepository extends Repository<AlarmNotifyRecordEntity, Long> {
    //
    AlarmNotifyRecordEntity save(AlarmNotifyRecordEntity entity);
    //
    AlarmNotifyRecordEntity getById(Long id);
    //
    List<AlarmNotifyRecordEntity> getAllByNotifyPersonnelIdAndRead(Long notifyPersonnelId, Integer read);
    //
    Boolean existsById(Long id);
    //
    Boolean existsByNotifyPersonnelIdAndAlarmRecordId(Long notifyPersonnelId, Long alarmRecordId);
    //
    AlarmNotifyRecordEntity getByNotifyPersonnelIdAndAlarmRecordId(Long notifyPersonnelId, Long alarmRecordId);
    //
    @Modifying
    @Query("update AlarmNotifyRecordEntity u set u.read = 1 where u.read = 0 and u.notifyPersonnelId = ?1")
    void markReadAllAlarmNotifyRecords(Long notifyPersonnelId);

    List<AlarmNotifyRecordEntity> getAllByNotifyPersonnelId(Long notifyPersonnelId);

    List<AlarmNotifyRecordEntity> getAllByAlarmRecordId(Long alarmRecordId);

    @Query(nativeQuery = true, value = "select distinct(notify_personnel_id) from alarm_notify_record where alarm_record_id in :alarmRecordIdList")
    List<Integer> getDistinctByNotifyPersonnelIdAndAlarmRecordIdIn(@Param("alarmRecordIdList") List<Long> alarmRecordIdList);

}
